﻿namespace Cmn
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class Js
    {
        public static void AddAttr(WebControl Control, string eventStr, string MsgStr)
        {
            Control.Attributes.Add(eventStr, MsgStr);
        }

        public static void Alert(string _Msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language=\"javascript\"> \n");
            builder.Append("alert(\"" + _Msg.Trim() + "\"); \n");
            builder.Append("</script>\n");
            HttpContext.Current.Response.Write(builder.ToString());
        }

        public static void Alert(Page page, string _Msg)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language=\"javascript\"> \n");
            builder.Append("alert(\"" + _Msg.Trim() + "\"); \n");
            builder.Append("</script>\n");
            page.RegisterClientScriptBlock("AlertJs", builder.ToString());
        }

        public static void Alert(Page page, string _Msg, bool isTop)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language=\"javascript\"> \n");
            builder.Append("alert(\"" + _Msg.Trim() + "\"); \n");
            builder.Append("</script>\n");
            if (isTop)
            {
                page.RegisterClientScriptBlock("AlertTopJs", builder.ToString());
            }
            else
            {
                page.RegisterStartupScript("AlertBottomJs", builder.ToString());
            }
        }

        public static bool chkFormData(Page page, string dataValue, string divObjStr, int minLength, int maxLength, string titleStr, bool isNVarchar)
        {
            int num = isNVarchar ? dataValue.Length : Str.cnLength(dataValue);
            StringBuilder builder = new StringBuilder();
            bool flag = true;
            if ((num == 0) && (minLength != 0))
            {
                if (divObjStr != "")
                {
                    builder.Append("document.getElementById(\"" + divObjStr + "\").innerHTML = \"<div class=ErrorMsg>" + titleStr + "不能为空！</div>\";");
                }
                else
                {
                    builder.Append("alert('" + titleStr + "不能为空！');");
                }
                flag = false;
            }
            else if (num < minLength)
            {
                if (divObjStr != "")
                {
                    builder.Append(string.Concat(new object[] { "document.getElementById(\"", divObjStr, "\").innerHTML = \"<div class=ErrorMsg>", titleStr, "不能小于", minLength, "个字符！</div>\";" }));
                }
                else
                {
                    builder.Append(string.Concat(new object[] { "alert('", titleStr, "不能小于", minLength, "个字符！');" }));
                }
                flag = false;
            }
            else if (num > maxLength)
            {
                if (divObjStr != "")
                {
                    builder.Append(string.Concat(new object[] { "document.getElementById(\"", divObjStr, "\").innerHTML = \"<div class=ErrorMsg>", titleStr, "不能大于", maxLength, "个字符！</div>\";" }));
                }
                else
                {
                    builder.Append(string.Concat(new object[] { "alert('", titleStr, "不能大于", maxLength, "个字符！');" }));
                }
                flag = false;
            }
            else
            {
                if (divObjStr != "")
                {
                    builder.Append("document.getElementById(\"" + divObjStr + "\").innerHTML = \"\");");
                }
                flag = true;
            }
            Run(page, builder.ToString(), false, titleStr);
            return flag;
        }

        public static bool chkFormData(Page page, string dataValue, Label lblObj, int minLength, int maxLength, string titleStr, bool isNVarchar)
        {
            int num = isNVarchar ? dataValue.Length : Str.cnLength(dataValue);
            if ((num == 0) && (minLength != 0))
            {
                lblObj.Text = titleStr + "不能为空！";
                return false;
            }
            if (num < minLength)
            {
                lblObj.Text = string.Concat(new object[] { titleStr, "不能小于", minLength, "个字符！" });
                return false;
            }
            if (num > maxLength)
            {
                lblObj.Text = string.Concat(new object[] { titleStr, "不能大于", maxLength, "个字符！" });
                return false;
            }
            lblObj.Text = "";
            return true;
        }

        public static void Import(Page page, string filePath, bool isTop)
        {
            StringBuilder builder = new StringBuilder();
            if (filePath.ToLower().Substring(filePath.Length - 3, 3) == ".js")
            {
                builder.Append("<script language=\"JavaScript\" src=\"" + filePath + "\" type=\"text/javascript\"></script>\n");
                string str = Regex.Match(filePath, @"\w+(?=\.\w+$)").Value.Trim();
                if (isTop)
                {
                    page.RegisterClientScriptBlock("TopJs" + Regex.Match(filePath, @"\w+(?=\.\w+$)").Value.Trim(), builder.ToString());
                }
                else
                {
                    page.RegisterStartupScript("BottomJs" + Regex.Match(filePath, @"\w+(?=\.\w+$)").Value.Trim(), builder.ToString());
                }
            }
            if (filePath.ToLower().Substring(filePath.Length - 4, 4) == ".css")
            {
                Run(page, "setStyle(\"" + filePath + "\");\n", isTop);
            }
        }

        public static void loadCss(PlaceHolder _PlaceHolder, string cssFile)
        {
            HtmlGenericControl child = new HtmlGenericControl("LINK");
            child.Attributes["rel"] = "stylesheet";
            child.Attributes["type"] = "text/css";
            child.Attributes["href"] = cssFile;
            _PlaceHolder.Controls.Add(child);
        }

        public static void Run(Page page, bool isTop, string IDStr)
        {
            if (isTop)
            {
                page.RegisterClientScriptBlock(IDStr, "");
            }
            else
            {
                page.RegisterStartupScript(IDStr, "");
            }
        }

        public static void Run(Page page, string _strCode, bool isTop)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language=\"javascript\">\n");
            builder.Append(_strCode.Trim());
            builder.Append("\n</script>\n");
            if (isTop)
            {
                page.RegisterClientScriptBlock("RunTopJs", builder.ToString());
            }
            else
            {
                page.RegisterStartupScript("RunBottomJs", builder.ToString());
            }
        }

        public static void Run(Page page, string _strCode, bool isTop, string IDStr)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<script language=\"javascript\">\n");
            builder.Append(_strCode.Trim());
            builder.Append("\n</script>\n");
            if (isTop)
            {
                page.RegisterClientScriptBlock(IDStr, builder.ToString());
            }
            else
            {
                page.RegisterStartupScript(IDStr, builder.ToString());
            }
        }
    }
}

